<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<c-nav-bar :title="titleHeader"></c-nav-bar>
		<view class="license">
			<view class="title">请输入有效的车牌号</view>
			<view class="boxs">
				<view :class="cindex===idx?'active':''" v-for="(i,idx) in 8" :key="i" @tap="selectLicense(idx)">
					<!-- <input type="text" :focus="cindex===idx?true:false" v-model="carnos[idx]"> -->
					{{carnos[idx]||''}}
					<view v-if="i==7" class="xny">新能源</view>
				</view>
			</view>
		</view>
		<view class="colors">
			<text>请选择车牌颜色</text>
			<view class="items">
				<view v-for="(item,index) in carcolors" :key="index" :class="yindex===index?'active':''"
					@tap="selectCarColor(item,index)">
					{{item}}
					<view class="gbg" v-if="yindex===index">
						<image class="g_bg" src="@/static/index/steamerTicket/icon_gbg.png"></image>
						<image class="g_gou" src="@/static/index/steamerTicket/icon_gou.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="add" @tap="addCarNo">新增</view>
		<u-keyboard mode="car" @change="valChange" @backspace="backspace" :show="show" @cancel="show=false"
			@confirm="show=false"></u-keyboard>
	</view>
</template>

<script>
	export default {
		name: 'inputCarInfo',
		data() {
			return {
				h: uni.getSystemInfoSync().windowHeight - 87,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				titleHeader: '添加车辆',
				cindex: '',
				yindex: '',
				carnos: [],
				show: false,
				carcolors: ['蓝牌', '黄牌', '绿牌', '黄绿牌', '黑牌', '白牌'],
				plateType: ''
			}
		},
		methods: {
			selectLicense(index) {
				this.cindex = index;
				this.show = true;
			},
			// 按键被点击(点击退格键不会触发此事件)
			valChange(val) {
				this.carnos = [...this.carnos, ...val.toString()];
				if (this.cindex < 7) this.cindex = this.cindex + 1;
				else this.cindex = 7;
			},
			// 退格键被点击
			backspace() {
				// 删除value的最后一个字符
				if (this.carnos.length) this.carnos.splice(this.carnos.length - 1, 1);
				if (this.cindex > 1) this.cindex = this.cindex - 1;
				else this.cindex = 0;
			},
			selectCarColor(item, index) {
				this.yindex = index;
				this.plateType = item;
			},
			addCarNo() {
				if (!this.$reg.carNo(this.carnos.join(''))) return this.$showToast('请输入有效的车牌号');
				if (!this.plateType) return this.$showToast('请选择车牌颜色');
				this.$api.post('/api/isplatenumber', {
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
					plateType: this.yindex,
					plateNumber: this.carnos.join('')
				}).then(res => {
					if (res.data.code === 0) {
						this.getOpenerEventChannel().emit('addCarNo', '');
						uni.navigateBack();
					} else this.$showModal(res.data.msg);
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		padding: 0 23rpx 186rpx;

		// padding-top: 130rpx;
		.license {
			padding-top: 120rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.title {
				font-size: 36rpx;
				font-family: PingFang-SC-Bold, PingFang-SC;
				font-weight: bold;
				color: #111111;
			}

			.boxs {
				width: 100%;
				margin-top: 64rpx;
				display: flex;
				align-items: center;
				justify-content: space-around;

				&>view {
					width: calc(100% / 7 - 10rpx);
					height: 90rpx;
					border: 2rpx solid #DEDEDE;
					position: relative;
					// padding: 12rpx 21rpx;
					margin: 0 5rpx;
					font-size: 34rpx;
					line-height: 90rpx;
					text-align: center;

					&.active::after {
						content: '';
						position: absolute;
						bottom: 6px;
						left: 15%;
						height: 2px;
						background-color: #2979ff;
						width: 70%;
					}

					input,
					uni-input {
						border: none;
						outline: none;
						width: 100%;
						height: 100%;
					}

					&.avtive::after {
						position: absolute;
						content: '';
						width: 100%;
						height: 4rpx;
						background: #007A69;
						bottom: 12rpx;
						left: 50%;
						margin-left: -50%;
					}

					.xny {
						width: 64rpx;
						height: 28rpx;
						background: #06B86F;
						border-radius: 14rpx;
						line-height: 28rpx;
						text-align: center;
						font-size: 16rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
						position: absolute;
						left: 50%;
						margin-left: -32rpx;
						top: -14rpx;
					}
				}
			}
		}

		.colors {
			margin-top: 72rpx;

			&>text {
				font-size: 30rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			.items {
				display: flex;
				align-items: center;
				justify-content: space-around;
				margin-top: 35rpx;

				&>view {
					width: calc(100% / 6 - 12rpx);
					height: 54rpx;
					background: #F3F4F6;
					border-radius: 8rpx;
					line-height: 54rpx;
					text-align: center;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					margin-left: 12rpx;
					position: relative;

					&:first-child {
						margin-left: 0;
					}

					.gbg {
						width: 22rpx;
						height: 16rpx;
						background: #FFFFFF;
						border-radius: 8rpx 0rpx 8rpx 0rpx;
						position: absolute;
						right: 0;
						bottom: 0;

						image {
							position: absolute;
						}

						.g_bg {
							z-index: 1;
							width: 100%;
							height: 100%;
						}

						.g_gou {
							z-index: 2;
							width: 9rpx;
							height: 6rpx;
							left: 50%;
							margin-left: -4.5rpx;
							top: 50%;
							margin-top: -3rpx;
						}
					}

					&.active {
						background: #007A69;
						color: #FFFFFF;
					}
				}
			}
		}

		.add {
			width: calc(100% - 48rpx);
			height: 92rpx;
			line-height: 92rpx;
			text-align: center;
			background: #007A69;
			border-radius: 46rpx;
			position: fixed;
			bottom: 64rpx;
			left: 24rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}
	}
</style>